package cn.zhqr.core.utils;

import cn.zhqr.common.util.collections.Lists;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;

import java.lang.reflect.Field;
import java.util.*;

public class BrandKit {
    public static final String DEFAULT_BRAND = BrandKit.BRAND_LXJX;
    public static final String DEFAULT_BRAND_NAME = "乐享鲸洗";
    private static ThreadLocal CUR_BRAND = new ThreadLocal();//当前品牌


    public static final String BRAND_LXJX = "lxjx";//乐享鲸洗
    public static final String BRAND_LTJX = "ltjx";//乐天鲸洗

    //内置品牌
    private static final List<String> INNER_BRANDS = Lists.arrayList(BRAND_LXJX);
    //全部品牌
    private static final List<String> ALL_BRANDS = Lists.arrayList(BRAND_LXJX, BRAND_LTJX);

    private static Map<String,BrandConfig> brandConf = new HashMap();

    public static boolean isExists(String brandCode){
        return StrKit.notBlank(brandCode) && ALL_BRANDS.contains(brandCode.toLowerCase());
    }

    public static final String getBrandNameByBrandCode(String code){
        String name = DEFAULT_BRAND_NAME;
        if (StrKit.notBlank(code)){
            BrandConfig config = getBrandConfig(code);
            if (StrKit.notBlank(code) && config != null && StrKit.notBlank(config.getBrandName())){
                name = config.getBrandName();
            }
        }
        return name;
    }

    public static BrandConfig getCurBrandConfig() {
        return getBrandConfig(null);
    }

    public static BrandConfig getBrandConfig(String brandCode) {
        if(StrKit.isBlank(brandCode)){
            brandCode = getThreadLocalBrandCode();
            if(StrKit.isBlank(brandCode)){
                brandCode = DEFAULT_BRAND;
            }
        }
        BrandConfig brandConfig = brandConf.get(brandCode);
        if(brandConfig == null){
            brandConfig = loadBrandConfig(brandCode);
            if(brandConfig != null){
                brandConf.put(brandCode,brandConfig);
            }
        }
        return brandConfig;
    }

    public static void setThreadLocalBrandCode(String brandCode) {
        CUR_BRAND.set(brandCode);
    }

    public static String getThreadLocalBrandCode() {
        return (String) CUR_BRAND.get();
    }


    public static Collection<BrandConfig> getAllBrandConfig(){
        return brandConf.values();
    }

    public static boolean isInnerBrand(String brandCode){
        if(StrKit.isBlank(brandCode)) return true;

        for(String innerBrandCode : INNER_BRANDS){
            if(innerBrandCode.equals(brandCode)){
                return true;
            }
        }
        return false;
    }

    /**
     * 比较两个品牌是否相同或者相通
     * @param brandCode1
     * @param brandCode2
     * @return
     */
    public static boolean compareBrands(String brandCode1,String brandCode2){
        brandCode1 = StrKit.isBlank(brandCode1)?DEFAULT_BRAND:brandCode1;
        brandCode2 = StrKit.isBlank(brandCode2)?DEFAULT_BRAND:brandCode2;

        if(isInnerBrand(brandCode1) && isInnerBrand(brandCode2)){
            return true;
        }
        return brandCode1.equals(brandCode2);
    }

    /**
     * 根据微信小程序appid获取对应配置
     * @param wechatMiniProgramAppId
     * @return
     */
    public static BrandConfig getBrandConfigByWechatMiniProgramAppId(String wechatMiniProgramAppId){
        if(StrKit.notBlank(wechatMiniProgramAppId)){
            for (String brand : ALL_BRANDS) {
                BrandConfig config = getBrandConfig(brand);
                if (config!=null && wechatMiniProgramAppId.equals(config.getWechatMiniProgramAppId())) {
                    return config;
                }
            }
        }
        return getBrandConfig(DEFAULT_BRAND);
    }

//    public static void registerBrand(String brand){
//        if(StrKit.notBlank(brand)){
//            BrandConfig config = getBrandConfig(brand);
//            if(config != null){
//                ALL_BRANDS.add(brand);
//            }
//        }
//    }



    public static String getWechatMiniProgramAppIdByBrandCode(String brandCode){
        BrandConfig brandConfig = getBrandConfig(brandCode);
        if(brandConfig!=null){
            return brandConfig.getWechatMiniProgramAppId();
        }
        return null;
    }

    public static String getWechatMchIdByBrandCode(String brandCode){
        BrandConfig brandConfig = getBrandConfig(brandCode);
        if(brandConfig!=null){
            return brandConfig.getWechatMchId();
        }
        return null;
    }

    private static BrandConfig loadBrandConfig(String brandCode){
        try{
            Prop prop = PropKit.use("brand/"+brandCode+".properties");
            BrandConfig config = new BrandConfig();
            Field[] fields = config.getClass().getDeclaredFields();
            for(Field field : fields){
                if(prop.containsKey(field.getName())){
                    field.setAccessible(true);

                    try {
                        if("int".equals(field.getType().getSimpleName()) || "Integer".equals(field.getType().getSimpleName())){
                            field.set(config,prop.getInt(field.getName()));
                        }else if("String".equals(field.getType().getSimpleName())){
                            field.set(config,prop.get(field.getName()));
                        }else if("boolean".equals(field.getType().getSimpleName())){
                            field.set(config,prop.getBoolean(field.getName()));
                        }
                    } catch (IllegalAccessException e) {
                        LogKit.error(brandCode+".properties "+field.getName()+"参数配置有误",e);
                    }
                }
            }
            return config;
        }catch (Exception e){
            LogKit.error("brand/"+brandCode+".properties 配置有误",e);
        }
        return null;
    }

    public static List<String> getAllBrands(){
        return ALL_BRANDS;
    }
    public static List<String> getAllInnerBrands(){
        return INNER_BRANDS;
    }
}

